<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<%@page import="au.com.mason.propertyTracker.domain.Property"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>

<script>
function Suburb(id, description) {
	this.id = id;
	this.description = description
}
var suburbs = new Array();
<c:forEach var="suburb" items="${suburbs}" varStatus="stat">
suburbs[<c:out value="${stat.count - 1}" />] = new Suburb(<c:out value="${suburb.id}" />, "<c:out value="${suburb.name}" />");
</c:forEach>
function populateSelect() {
	var preSelect = -1;
	<c:if test="${displayPropertiesForm.searchParameters.suburbId != null}">
	preSelect = <c:out value="${displayPropertiesForm.searchParameters.suburbId}" />;
	</c:if>
	for (count = 0; count < suburbs.length; count++) {
		document.getElementById("searchParameters.suburbId").options[document.getElementById("searchParameters.suburbId").options.length] = new Option(suburbs[count].description, suburbs[count].id);
		if (suburbs[count].id == preSelect) {
			document.getElementById("searchParameters.suburbId").selectedIndex = document.getElementById("searchParameters.suburbId").options.length - 1; 
		}
	}
}
function filterSelect(enteredText) {
	document.getElementById("searchParameters.suburbId").options.length = 0;
	for (count = 0; count < suburbs.length; count++) {
		if (suburbs[count].description.toLowerCase().indexOf(enteredText.toLowerCase()) != -1) {
			document.getElementById("searchParameters.suburbId").options[document.getElementById("searchParameters.suburbId").options.length] = new Option(suburbs[count].description, suburbs[count].id);
		}
	}
	if (document.getElementById("searchParameters.suburbId").length == 1) {
		document.getElementById("searchParameters.suburbId").selectedIndex = 0
	}
}
</script>

<%try { %>
<head>
<link href='includes/css/tooltip.css' rel="stylesheet" type="text/css">
<script type="text/javascript" language="javascript" src="includes/js/tooltip.js"></script>
<script type="text/javascript" language="javascript" src="includes/js/main.js"></script>
<script>
var imageSet = false;
var page = <c:out value="${displayPropertiesForm.searchParameters.page}" />;
</script>
</head>

<form:form action="AutoAddProperty.action" commandName="autoAddPropertyForm">
<table align="center" width="85%">
<tr>
<td width="20%"><a href="javascript:saveSearchResults('addProperty')">Add Property</a></td>
<td width="20%"><a href="javascript:saveSearchResults('addRentalProperty')">Add Rental</a></td>
<td width="20%"><a href="javascript:saveSearchResults('referenceData')">Reference Data</a></td>
<td width="10%"><a href="javascript:saveSearchResults('autoAddProperty')">Auto Add Multiple</a></td>
<td width="10%"><a href="javascript:saveSearchResults('showReport')">Show Report</a></td>
<td width="20%"><input type="text" name="urls[0]" /><input type="button" value="Auto Add" onclick="this.form.submit()"></td>
</tr>
</table>
</form:form>
<br>

<c:if test="${messages != null}">
	<c:forEach items="${messages}" var="message">
		<font color="red"><c:out value="${message}" /></font><br>
	</c:forEach>
</c:if>

<form:form action="SubmitFilterProperties.action" commandName="displayPropertiesForm">
<form:hidden path="searchParameters.page" />
<form:hidden path="redirect" />
<form:hidden path="propertyId" />
<table width="100%">
<tr>
<td width="20%" valign="top">
	<table width="100%" cellpadding="5">
		<!-- tr>
			<td>Above Price:<form:input path="searchParameters.priceFrom"/></td><td>Below Price:<form:input path="searchParameters.priceTo"/></td>
		</tr -->
		<tr>
			<td valign="top">Suburb</td>
			<td><input type="text" name="filterSuburb" onkeyup="filterSelect(this.value)"><br>
			<form:select path="searchParameters.suburbId" size="5">
			</form:select>
			</td>
		</tr>
		<tr>
			<td>Region</td>
			<td>
			<form:select path="searchParameters.regionId">
				<form:option value="" label="Not Selected"/>
				<form:options items="${regions}" itemValue="id" itemLabel="name"/>
			</form:select>
		</tr>
		<tr>
			<td colspan="2">
			<table>
			<tr>
			<td align="right">Under Contract&nbsp;<form:checkbox path="searchParameters.underContract" value="true" /></td>
			<td align="right">Rental&nbsp;<form:checkbox path="searchParameters.rental" value="true" /></td>
			</tr>
			<tr>
			<td align="right">Removed&nbsp;<form:checkbox path="searchParameters.removed" value="true" /></td>
			<td align="right">Sold&nbsp;<form:checkbox path="searchParameters.sold" value="true" /></td>
			</tr>
			<tr>
			<td align="right">Price Changed&nbsp;<form:checkbox path="searchParameters.priceChanged" value="true" /></td>
			</tr>
			</table>
		</tr>		
		<tr>
			<td><input type="submit" value="Filter" onclick="this.form['searchParameters.page'].value=1;this.form.submit()">&nbsp;<input type="button" value="Clear" onclick="document.location.href='SubmitFilterProperties.action'"></td>
		</tr>
	</table>
</td>
<td width="80%" valign="top">
<table width="100%" valign="top">
<tr>
<td width="70%" valign="top">
<table align="left" width="100%" cellpadding="5" valign="top" border=0>
	<tr>
		<td width="45%"><b>Address</b></td>
		<td><b>Status</b></td>
		<c:if test="${!displayPropertiesForm.searchParameters.rental}">
		<td><b>From($)</b></td>
		<td><b>To($)</b></td>
		</c:if>
		<td><b>Bed</b></td>
		<td><b>Bath</b></td>
		<td><b>Car</b></td>
		<td width="15%"><b>Date</b></td>
		<td><b>Rent($)</b></td>
		<c:if test="${!displayPropertiesForm.searchParameters.rental}">
		<td><b>Tenanted</b></td>
		<td><b>Yield(%)</b></td>
		</c:if>
		<c:if test="${displayPropertiesForm.searchParameters.rental}">
		<td><b>Furnished</b></td>
		</c:if>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<% for (Iterator iter = ((List)request.getAttribute("properties")).iterator(); iter.hasNext();) {
	Property property = (Property) iter.next();
	%>
	<tr>
		<td><span onmouseover="tooltip.show('<%=property.getDescriptionString()%>', 200);changeImage('<%=property.getImageUrl()%>');showMap('<%=property.getMapString()%>');showComments('<%=property.getCommentsString()%>')" onmouseout="tooltip.hide();"><%=property.getStreetAddress()%> <%if (property.getSuburb() != null) {%><%=property.getSuburb().getName()%><%}%></span></td>
		<td><%if (property.isUnderContract()) {%><img src="images/yellowSquare.jpg"><%} else if (property.isRemoved()) {%><img src="images/redSquare.jpg"><%} else if (property.isSold()) {%><img src="images/greenSquare.jpg"><%}%></td>
		<c:if test="${!displayPropertiesForm.searchParameters.rental}">
		<td><%=property.getPriceFromString()%></td>
		<td><%=property.getPriceToString()%></td>
		</c:if>
		<td><%=property.getNumberBedrooms()%></td>
		<td><%=property.getNumberBathrooms()%></td>
		<td><%=property.getNumberCarSpacesString()%></td>
		<td><%=property.getDateListedString()%></td>
		<td><%=property.getRentString()%></td>
		<c:if test="${!displayPropertiesForm.searchParameters.rental}">
		<td><%=property.isCurrentlyTenantedString()%></td>
		<td><%=property.getYieldString()%></td>
		</c:if>
		<c:if test="${displayPropertiesForm.searchParameters.rental}">
		<td><%=property.getFurnished()%></td>
		</c:if>
		<td><a href="javascript:OpenWindow('<%=property.getUrl()%>')">View</a></td>
		<td><a href="javascript:document.forms[1].propertyId.value=<%=property.getId()%>;saveSearchResults('editProperty')">Edit</a></td>
		<td><a href="javascript:document.forms[1].propertyId.value=<%=property.getId()%>;saveSearchResults('deleteProperty')">Delete</a></td>
	</tr>
	<%}%>
	<tr>
	<td colspan="<c:if test="${!displayPropertiesForm.searchParameters.rental}">11</c:if><c:if test="${displayPropertiesForm.searchParameters.rental}">7</c:if>"><c:if test="${displayPropertiesForm.searchParameters.showPrevious}"><a href="javascript:previousPage()">Previous</a></c:if><c:if test="${displayPropertiesForm.searchParameters.showNext}">&nbsp;<a href="javascript:nextPage()">Next</a></c:if></td>
	<td colspan=3><c:out value="${displayPropertiesForm.searchParameters.pageResults}" /></td>
	</tr>
	<tr>
	<td><b>Comments:</b></td><td colspan="<c:if test="${!displayPropertiesForm.searchParameters.rental}">13</c:if><c:if test="${displayPropertiesForm.searchParameters.rental}">9</c:if>"><div id="comments"></div></td>
	</tr>
	<tr>
	<td colspan="<c:if test="${!displayPropertiesForm.searchParameters.rental}">14</c:if><c:if test="${displayPropertiesForm.searchParameters.rental}">10</c:if>"><img src="images/yellowSquare.jpg">&nbsp;Under Contract&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/redSquare.jpg">&nbsp;Removed&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/greenSquare.jpg">&nbsp;Sold&nbsp;</td>
	</tr>
</table>
</td>
<td width="30%" valign="top"><div id="imageUrl"></div>
<br>
<div id="mapUrl"></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form:form>

<%} catch (Exception e) {
	e.printStackTrace();%>
	<font color=red>THERE WAS AN EXCEPTION</font>
<%}
%>
<script>populateSelect()</script>
</html>